package com.gree.scmservice.service.saas.impl;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.common.utils.MinIoUtil;
import com.gree.common.utils.StringUtils;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.MenuDao;
import com.gree.scmservice.dao.saasDao.SaasAddonDao;
import com.gree.scmservice.dao.saasDao.SaasMenuAddonDao;
import com.gree.scmservice.entity.saas.SaasMenuAddon;
import com.gree.scmservice.entity.saas.SaasSysAddon;
import com.gree.scmservice.entity.saas.vo.SaasSysAddonVo;
import com.gree.scmservice.entity.smbse.SysMenuMo;
import com.gree.scmservice.service.GscmMenuService;
import com.gree.scmservice.service.saas.SaasAddonService;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SaasAddonServiceImpl implements SaasAddonService {

    @Resource
    private SaasAddonDao saasAddonDao;

    @Resource
    private SaasMenuAddonDao saasMenuAddonDao;

    @Resource
    private MenuDao menuDao;

    @Resource
    private GscmMenuService gscmMenuService;

    @Override
    public int addPlugin(SaasSysAddon saasSysAddon) {
        saasSysAddon.setStatus(1);
        saasSysAddon.setVersion("1.0");
        saasSysAddon.setCreate_time(LocalDateTime.now());
        SaasSysAddon insert = saasAddonDao.insertSaasSysAddon(saasSysAddon);
        return insert != null?1:0;
    }

    @Override
    public int updatePlugin(SaasSysAddonVo saasSysAddonVo) {
        SaasSysAddon saasSysAddon = saasSysAddonVo;
        List<SaasMenuAddon> saasMenuAddonList = saasSysAddonVo.getSaasMenuAddonList();
        saasMenuAddonDao.deleteSaasMenuAddonById(saasSysAddonVo.getAddon_id());
        saasMenuAddonList.forEach(saasMenuAddon -> {
            saasMenuAddonDao.insertSaasMenuAddon(saasMenuAddon);
        });
        return saasAddonDao.updateSaasSysAddon(saasSysAddon);
    }

    @Override
    public int deletePlugin(Long id) {
        return saasAddonDao.deleteSaasSysAddonById(id);
    }

    @Override
    public int deleteSaasSysAddonByIds(Long[] addonIds)
    {
        return saasAddonDao.deleteSaasSysAddonByIds(addonIds);
    }

    @Override
    public TablePageData<SaasSysAddon> selectPluginPage(SaasSysAddon saasSysAddon) {
        TablePageData<SaasSysAddon> tablePageData = saasAddonDao.selectSaasSysAddonList(saasSysAddon);
        return tablePageData;
    }

    @Override
    public SaasSysAddon selectById(Long id) {
        return saasAddonDao.selectSaasSysAddonById(id);
    }

    @Override
    public ReturnVo uploadIcon(MultipartFile file, String addonId) {
        String upload = MinIoUtil.upload("saas/addon", "gscm", file);
        if(upload!=null){
            saasAddonDao.updateSaasAddonIcon(addonId, upload);
            return ReturnVo.success(upload);
        }
        return ReturnVo.error();
    }

    @Override
    public void downloadIcon(String icon, HttpServletResponse response) {
        MinIoUtil.download("gscm", icon, response);
    }

    @Override
    public int menuInsertAddUpdate(String addon_id, String[] tenos) {
        int result = 0;
        saasMenuAddonDao.deleteSaasMenuAddonById(Long.valueOf(addon_id));
        for(String teno:tenos){
            SaasMenuAddon saasMenuAddon = new SaasMenuAddon();
            saasMenuAddon.setAddonId(addon_id);
            saasMenuAddon.setTeno(teno);
            SaasMenuAddon insert = saasMenuAddonDao.insertSaasMenuAddon(saasMenuAddon);
            result+= insert != null?1:0;
        }
        return result;
    }

    @Override
    public ArrayList getMenuFromAddon() {
        String tenantId = SecurityUtils.getUserWithPasswordMode().get("username").toString();
        List<SysMenuMo> list = new ArrayList<>();
        ArrayList result = new ArrayList();
        String filter =" and sb06n.styp = 4 and sb07n.tono in (select teno from SAAS_MENU_ADDON t1, SAAS_TENANT_ADDON t2 where t1.addon_id = t2.addon_id and t2.tenant_id = '"+tenantId+"')";
        selectMenuListRecur("0",filter,list);
        result.add(gscmMenuService.buildMenuTreeSelect(list));
        return result;
    }

    @Override
    public List<Integer> selectMenuListByAddonId(Long AddonId) {
        List<Integer> result = new ArrayList<>();
        List<SaasMenuAddon> list = saasMenuAddonDao.selectSaasMenuAddonById(AddonId);
        for(SaasMenuAddon saasMenuAddon : list){
            result.add(Integer.valueOf(saasMenuAddon.getTeno()));
        }
        return result;
    }

    public void selectMenuListRecur(String teno,String filter,List dicList) {
        List<Record> list = menuDao.findMenuByFono(teno,filter);
        for (Record record : list) {
            List<SysMenuMo> childList = new ArrayList<SysMenuMo>();
            if (record != null) {
                SysMenuMo sysMenuMo = new SysMenuMo();
                sysMenuMo.setOrderNum(record.getString("TASC"));
                sysMenuMo.setPath(record.getString("TURL"));
                sysMenuMo.setMenuName(record.getString("TNAM"));
                sysMenuMo.setDsca(record.getString("TDSC"));
                sysMenuMo.setIcon(record.getString("TIMG"));
                sysMenuMo.setStyp(record.getInt("STYP"));
                sysMenuMo.setMenuId(record.getLong("TONO"));
                sysMenuMo.setParentId(record.getLong("FONO"));
                sysMenuMo.setPano(record.getString("PANO"));
                selectMenuListRecur(record.getString("TENO"),filter,childList);
                sysMenuMo.setChildren(childList);
                dicList.add(sysMenuMo);
            }
        }
    }

}
